package com.dong.websocket.handler;

import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.tio.core.ChannelContext;
import org.tio.core.Tio;
import org.tio.core.TioConfig;
import org.tio.http.common.HttpRequest;
import org.tio.http.common.HttpResponse;
import org.tio.websocket.common.WsRequest;
import org.tio.websocket.common.WsResponse;
import org.tio.websocket.server.handler.IWsMsgHandler;

/**
 * @Author 955
 * @Date 2023-07-31 18:26
 * @Description
 */
@Slf4j
@Component
public class TioWsMsgHandler implements IWsMsgHandler {
    /**
     * TIO-WEBSOCKET 配置信息
     */
    public static TioConfig serverTioConfig;

    @Override
    public HttpResponse handshake(HttpRequest httpRequest, HttpResponse httpResponse, ChannelContext channelContext) throws Exception {
        serverTioConfig = channelContext.tioConfig;
        return httpResponse;
    }

    @Override
    public void onAfterHandshaked(HttpRequest httpRequest, HttpResponse httpResponse, ChannelContext channelContext) throws Exception {
        // 拿到用户id
        String id = httpRequest.getParam("id");
        // 绑定用户
        Tio.bindUser(channelContext, id);

        // 绑定业务类型(根据业务类型判定处理相关业务)
        String bsId = httpRequest.getParam("bsId");
        if (!StringUtils.isEmpty(bsId)) {
            Tio.bindBsId(channelContext, bsId);
        }
        // 给用户发送消息
        WsResponse wsResponse = WsResponse.fromText("您已成功连接 WebSocket 服务器", "UTF-8");
        Tio.sendToUser(channelContext.tioConfig, id, wsResponse);
    }

    @Override
    public Object onBytes(WsRequest wsRequest, byte[] bytes, ChannelContext channelContext) throws Exception {
        return null;
    }

    @Override
    public Object onClose(WsRequest wsRequest, byte[] bytes, ChannelContext channelContext) throws Exception {
        // 关闭连接
        Tio.remove(channelContext, "WebSocket Close");
        return null;
    }

//    @Override
//    public Object onText(WsRequest wsRequest, String s, ChannelContext channelContext) throws Exception {
//        WsResponse wsResponse = WsResponse.fromText("服务器已收到消息：" + s, "UTF-8");
//        Tio.sendToUser(channelContext.tioConfig, "10000", wsResponse);
//        return null;
//    }

    @Override
    public Object onText(WsRequest wsRequest, String text, ChannelContext channelContext) throws Exception {
        JSONObject message = JSONObject.parseObject(text);
        log.info("收到客户端的消息是:{}",message);
        log.info("收到客户端的id是:{}",channelContext.getToken());
        // 接收消息的用户ID
        String receiver = message.getString("receiver");
        // 发送消息者
        String sendName = message.getString("sendName");
        // 消息
        String msg = message.getString("msg");

        // 保存聊天记录到DB等业务逻辑...

        WsResponse wsResponse = WsResponse.fromText(message.toString(), "UTF-8");
        Tio.sendToUser(channelContext.tioConfig, receiver, wsResponse);

        JSONObject resp = new JSONObject();
        resp.put("sendName", "系统提醒");
        resp.put("msg", "发送成功");
        return resp.toString();

    }
}

